package cn.s1995.sloth.bms.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.s1995.sloth.bizs.entity.OrderAddress;
import cn.s1995.sloth.bizs.service.OrderAddressService;
import cn.s1995.sloth.common.core.util.R;
import cn.s1995.sloth.common.excel.annotation.ResponseExcel;
import cn.s1995.sloth.common.log.annotation.SysLog;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 订单地址
 *
 * @author sloth
 * @date 2023-12-15 15:28:42
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/orderAddress")
@Tag(description = "OrderAddress", name = "BMS-订单地址管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class OrderAddressController {

    private final OrderAddressService orderAddressService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param req 订单地址
     * @return
     */
	@ApiOperationSupport(order = 1)
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('bizs_OrderAddress_view')")
    public R getPage(@ParameterObject Page page, @ParameterObject OrderAddress req) {
        LambdaQueryWrapper<OrderAddress> wrapper = Wrappers.lambdaQuery();
        return R.ok(orderAddressService.page(page, wrapper));
    }


    /**
     * 通过id查询订单地址
     * @param id id
     * @return R
     */
	@ApiOperationSupport(order = 1)
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('bizs_OrderAddress_view')")
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(orderAddressService.getById(id));
    }

    /**
     * 新增订单地址
     * @param req 订单地址
     * @return R
     */
	@ApiOperationSupport(order = 10)
    @Operation(summary = "新增订单地址", description = "新增订单地址")
    @SysLog("新增订单地址")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('bizs_OrderAddress_add')")
    public R save(@RequestBody OrderAddress req) {
        return R.ok(orderAddressService.save(req));
    }

    /**
     * 修改订单地址
     * @param req 订单地址
     * @return R
     */
	@ApiOperationSupport(order = 20)
    @Operation(summary = "修改订单地址", description = "修改订单地址")
    @SysLog("修改订单地址")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('bizs_OrderAddress_edit')")
    public R updateById(@RequestBody OrderAddress req) {
        return R.ok(orderAddressService.updateById(req));
    }

    /**
     * 通过id删除订单地址
     * @param ids id列表
     * @return R
     */
	@ApiOperationSupport(order = 30)
    @Operation(summary = "通过id删除订单地址", description = "通过id删除订单地址")
    @SysLog("通过id删除订单地址")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('bizs_OrderAddress_del')")
    public R remove(@RequestBody Long[] ids) {
        return R.ok(orderAddressService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param req 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
	@ApiOperationSupport(order = 40)
    @Operation(summary = "导出excel表格", description = "")
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('bizs_OrderAddress_export')")
    public List<OrderAddress> export(OrderAddress req, Long[] ids) {
        return orderAddressService.list(Wrappers.lambdaQuery(req).in(ArrayUtil.isNotEmpty(ids), OrderAddress::getId, ids));
    }
}
